<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Agent extends Model
{
    /**
     * 获取代理列表
     *
     * @return void
     */
    public function getList($get)
    {
        if (is_numeric($get['status'])) {
			$map['agent.status'] = $get['status'];
		} 
		if (isset($get['dltype'])) {
			$map['dltype'] = $get['dltype'];
		}
		if (isset($get['province'])) {
			$map['agent.province'] = $get['province'];
		}
		if (isset($get['keywords'])) {
			$map['name|tel'] = ['like', '%'.$get['keywords'].'%'];
		}

		$list = Db::view('agent','*')
			->view('user','name,tel','agent.uid=user.uid')
			->where($map)
			->paginate();

		return $list;
    }

    /**
     * 获取代理信息 
     *
     * @param [type] $id
     * @return void
     */
    public function getInfo($id)
    {
        $info = Db::view('agent', '*')
            ->view('user', 'name,tel', 'agent.uid=user.uid')
            ->where('id', $id)
            ->find();

        return $info;
    }

    /**
     * 移除代理
     *
     * @return void
     */
    public function rmAgent($id)
    {
        $uid = $this->where('id', $id)->value('uid');
        db('user')->where('uid', $uid)->setField('is_agent', 0);
        $rst = $this->where('id', $id)->delete();
        if ($rst) {
            return ['code' => 1, 'msg' => '已经移除！'];
        }
        return ['code' => 0, 'msg' => '稍后重试'];
    }

    /**
     * 添加代理
     *
     * @param [type] $data
     * @return void
     */
    public function addInfo($data)
    {
        $user = db('user')->where('tel', $data['tel'])->field('uid')->find();
        if (! $user) {
             return ['code' => 0, 'msg' => '此会员不存在'];
        }

        if ($this->where('uid', $user['uid'])->count()) {
            return ['code' => 0, 'msg' => '此会员已经是代理'];
        }
        
        $where = [
            'province' => $data['province'],
            'city' => $data['city'],
            'status' => 1
        ];

        if ($this->where($where)->count()) {
            return ['code' => 0, 'msg' => '此区域代理已存在，请选择其他区域!'];
        }

        $where['uid'] = db('user')->where('tel', $data['tel'])->value('uid');
        $where['mer_name'] = $this->getAreaName($data['province'], $data['city']);

        if ($this->save($where)) {
            db('user')->where('uid', $where['uid'])->setField('is_agent', 1);
            return ['code' => 1, 'msg' => '已添加'];
        }

        return ['code' => 0, 'msg' => '稍后重试'];
    }

    /**
     * 代理编辑
     *
     * @param [type] $data
     * @return void
     */
    public function editInfo($data)
    {
        if ($data['id']) {
            $info = $this->where('id', $data['id'])->find();

            if ($data['province'] != $info['province'] || $data['city'] != $info['city']) {
                $where = [
                    'province' => $data['province'],
                    'city' => $data['city'],
                    'status' => 1
                ];

                if ($this->where($where)->count()) {
                    return ['code' => 0, 'msg' => '此区域代理已存在，请选择其他区域!'];
                }

                $where['uid'] = $data['uid'];
                $where['mer_name'] = $this->getAreaName($data['province'], $data['city']);

                if ($this->save($where)) {
                    return ['code' => 1, 'msg' => '已添加'];
                }
            }
            return ['code' => 0, 'msg' => '您未做修改'];
        }
        return ['code' => 0, 'msg' => '稍后重试'];
    }

    /**
	 * 返回区域名称
	 * 
	 * @param  [type] $p 省区域代码
	 * @param  [type] $c 市区域代码
	 * @param  string $d 区域代码
	 * @return [type]    代理类型
	 */
	public function getAreaName($p, $c, $d = '', $dltype = 1)
	{

		$p = db('cn_area')->where(['p_code' => $p, 'is_p' => 1])->value('name');
		$c = db('cn_area')->where(['c_code' => $c, 'is_c' => 1])->value('name');
		if ($d && $dltype == 2) {
			$d = '-' . db('cn_area')->where('code', $d)->value('name');
		} else {
			$d = '';
		}

        return $p . '-' . $c . $d;
	}
}
